package com.ljkj.cordial.chat.util;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.text.TextUtils;

import com.ljkj.cordial.chat.dao.AlbumTableBean;
import com.nic.view.util.FileSizeUtil;
import com.yanzhenjie.album.AlbumFile;
import com.yanzhenjie.album.util.AlbumUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

public class FileProvider {
    private Context context;

    public FileProvider(Context context) {
        this.context = context;
    }

    public List<AlbumFile> getList(String... str) {
        List<AlbumFile> list = null;
        if (context != null) {
            String[] projection = new String[]{MediaStore.Files.FileColumns._ID,
                    MediaStore.Files.FileColumns.DATA,
                    MediaStore.Files.FileColumns.SIZE
            };

            StringBuffer likes = new StringBuffer();
            for (int i = 0; i < str.length; i++) {
                if (i == 0) {
                    likes.append(MediaStore.Files.FileColumns.DATA + " like ?");
                } else {
                    likes.append(" or " + MediaStore.Files.FileColumns.DATA + " like ?");
                }
            }
            //                    "regexp_like(" + MediaStore.Files.FileColumns.DATA + ", '" + likes + "')",
            Cursor cursor = context.getContentResolver().query(
                    Uri.parse("content://media/external/file"),
                    projection,
                    likes.toString(),
                    str,
                    null);
            if (cursor != null) {
                list = new ArrayList<>();
                while (cursor.moveToNext()) {
//                    int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));//ID
                    String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));//路径
                    long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));//大小
//                    long duration = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));//时长
                    if (size == 0L || TextUtils.isEmpty(path)) {
                        continue;
                    }
                    File f = new File(path);

                    if (f.length() == 0L || (!AlbumTableBean.isRecordFile(path) && (FileSizeUtil.getFileOrFilesSize(path, FileSizeUtil.SIZETYPE_KB) < 60.0d || FileSizeUtil.getFileOrFilesSize(path, FileSizeUtil.SIZETYPE_MB) > 100.0d))) {
                        continue;
                    }
                    AlbumFile albumFile = new AlbumFile();
                    albumFile.setMimeType(AlbumUtils.getMimeType(path));
                    albumFile.setAddDate(new Date(f.lastModified()).getTime());
                    albumFile.setPath(path);
                    albumFile.setSize(size);
//                    albumFile.setDuration(duration);
                    list.add(albumFile);
                }
                cursor.close();
            }
        }
        Collections.sort(list, comparator);
        return list;
    }

    private Comparator<AlbumFile> comparator = new Comparator<AlbumFile>() {
        @Override
        public int compare(AlbumFile o1, AlbumFile o2) {
            if (o1.getAddDate() > o2.getAddDate()) {
                return -1;
            }
            if (o1.getAddDate() < o2.getAddDate()) {
                return 1;
            }
            return 0;
        }
    };
}
